#coding=utf-8
'''
有一个字典列表，你想根据某个或某几个字典字段来排序这个列表
itemgetter
'''
rows = [
    {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
    {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
    {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
    {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]

from operator import itemgetter
rows_by_fname = sorted(rows, key=itemgetter('fname'))
rows_by_uid = sorted(rows, key=itemgetter('uid'))
print(rows_by_fname)
print(rows_by_uid)

# The  itemgetter() function can also accept multiple keys.
rows_by_lfname = sorted(rows, key=itemgetter('lname', 'fname'))
print(rows_by_lfname)

# The functionality of  itemgetter() is sometimes replaced by  lambda expressions.
rows_by_fname = sorted(rows, key=lambda r: r['fname'])
rows_by_lfname = sorted(rows, key=lambda r: (r['lname'], r['fname']))

# Last, but not least, don’t forget that the technique shown in this recipe can be applied
# to functions such as  min() and  max().
a = min(rows, key=itemgetter('uid'))
b = max(rows, key=itemgetter('uid'))
print(a)
print(b)